package com.buildings.activity;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.bigkoo.pickerview.OptionsPickerView;
import com.buildings.R;
import com.buildings.api.Api;
import com.buildings.api.Constant;
import com.buildings.base.BaseActivity;
import com.buildings.base.BaseFormActivity;
import com.buildings.bean.AddressBean;
import com.buildings.bean.ImageURL;
import com.buildings.bean.LoginBean;
import com.buildings.bean.TownHouseBean;
import com.buildings.bean.TownHouseResultBean;
import com.buildings.bean.VillageHouseBean;
import com.buildings.bean.VillageHouseResultBean;
import com.buildings.http.BaseCallBack;
import com.buildings.http.JsonBuilder;
import com.buildings.http.MyHttpClient;
import com.buildings.util.CustomToast;
import com.buildings.util.DataUtils;
import com.buildings.util.DialogUtils;
import com.buildings.util.FormDataUtil;
import com.buildings.util.PickViewUtil;
import com.buildings.util.SPUtil;
import com.buildings.util.Utils;
import com.bumptech.glide.Glide;
import com.google.gson.Gson;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.text.Normalizer;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;

/**
 * 农村住宅表单
 */
public class VillageHouseFormActivity extends BaseFormActivity implements View.OnClickListener {

    public static final String VILLAGE_HOUSE = "villageHouse";

    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.tv_right_text)
    TextView tvSave;
    @BindView(R.id.tv_left_text)
    TextView tvClear;
    @BindView(R.id.et_building_number)
    EditText etNumber;//建筑物编号
    @BindView(R.id.et_lng)
    EditText etLng;//经度
    @BindView(R.id.et_lat)
    EditText etLat;//纬度
    @BindView(R.id.et_building_type)
    EditText etBuildingType;//被调查建筑物分类
    @BindView(R.id.et_province)
    EditText etProvince;//省市区
    @BindView(R.id.et_city)
    EditText etCity;//省市区
    @BindView(R.id.et_area)
    EditText etArea;//省市区
    @BindView(R.id.et_building_xiangzhen)
    EditText etBuidingXiangZhen;//乡镇
    @BindView(R.id.et_xingzhengcun)
    EditText etXingZhengCun;//行政村
    @BindView(R.id.et_building_name)
    EditText etBuildingName;//建筑名称
    @BindView(R.id.et_building_address)
    EditText etBuildingAddress;//建筑地址
    @BindView(R.id.et_huzhuxinxi)
    EditText etHuZhuXinXi;//户主信息
    @BindView(R.id.et_jiegouleixing)
    EditText etJieGouLeiXing;//结构类型
    @BindView(R.id.et_jiegouleixing_qita)
    EditText getEtJieGouLeiXingQiTa;//结构类型其他
    @BindView(R.id.et_jianzaofangshi)
    EditText etJianZaoFangShi;//建造方式
    @BindView(R.id.et_jianzaofangshi_qita)
    EditText etJianZaoFangShiQiTa;//建造方式其他
    @BindView(R.id.et_zhaijidimianji)
    EditText etZhaiJiDiMianJi;//宅基地面积

    @BindView(R.id.et_jianzhumianji)
    EditText etJianZhuMianJi;//建筑面积
    @BindView(R.id.et_jianzhucengshu)
    EditText etJianZhuCengShu;//建筑层数

    @BindView(R.id.et_jianzhujianshu)
    EditText etJianZhuJianShu;//建筑间数

    @BindView(R.id.et_jianzhugaodu)
    EditText etJianZhuGaoDu;//建筑高度

    @BindView(R.id.et_jianzaoshijian)
    EditText etJianZaoShiJian;//建造时间

    @BindView(R.id.et_renkoushu)
    EditText etRenKouShu;//常驻人口数

    @BindView(R.id.et_shiyongqingkuang)
    EditText etShiYongQingKuang;//房屋使用情况
    @BindView(R.id.et_shiyongqingkuang_qita)
    EditText etShiYongQingKuangQiTa;//房屋使用情况其他


    @BindView(R.id.et_kangzhenjiagu)
    EditText etKangZhenJiaGu;//抗震加固情况

    @BindView(R.id.et_kangzhengouzaocuoshi)
    EditText etKangZhenGouZao;//抗震构造措施
    @BindView(R.id.et_gouzaocuoshiqingkuang)
    EditText etGouZaoCuoShiQingKuang;//抗震构造措施情况
    @BindView(R.id.et_gouzaocuoshiqingkuang_qita)
    EditText etGouZaoCuoShiQingKuangQiTa;//抗震构造措施情况其他
    @BindView(R.id.ll_gouzaocuoshiqingkuang)
    LinearLayout llGouZaoCuoShiQingKuang;

    @BindView(R.id.et_liefengbianxing)
    EditText etLieFengBianXing;//裂缝变形倾斜
    @BindView(R.id.et_liefengbianxing_qita)
    EditText etLieFengBianXingQiTa;//裂缝变形倾斜其他

    @BindView(R.id.et_shouzaiqingkuang)
    EditText etShouZaiQingKuang;//受灾情况
    @BindView(R.id.et_cengshouzaiqingkuang)
    EditText etCengShouZaiQingKuang;//曾受灾情况
    @BindView(R.id.et_cengshouzaiqingkuang_qita)
    EditText etCengShouZaiQingKuangQiTa;//曾受灾情况补充
    @BindView(R.id.et_cengshouzaiqingkuang_remark)
    EditText etCengShouZaiQingKuangRemark;//曾受灾情况备注
    @BindView(R.id.ll_cengshouzaiqingkuang)
    LinearLayout llCengShouZaiQingKuang;
    @BindView(R.id.ll_shouzaibeizhu)
    LinearLayout llShouZaiBeiZhu;//受灾备注


    @BindView(R.id.et_fuzhuyongfang)
    EditText etFuZhuYongFang;//是否有辅助用房
    @BindView(R.id.ll_fuzhuyongfang)
    LinearLayout llFuZhuYongFang;

    @BindView(R.id.et_fuzhuyongtu)
    EditText etFuZhuYongTu;//辅助用房用途
    @BindView(R.id.et_fuzhuyongtu_qita)
    EditText etFuZhuYongTuQiTa;//是否有辅助用房

    @BindView(R.id.et_fuzhujianzaofangshi)
    EditText etFuZhuJianZaoFangShi;//辅助用房建造方式
    @BindView(R.id.et_fuzhujianzaofangshi_qita)
    EditText etFuZhuJianZaoFangShiQiTa;//辅助用房建造方式其他

    @BindView(R.id.et_fuzhugouzaoleixing)
    EditText etFuZhuGouZaoLeiXing;//辅助用房构造类型
    @BindView(R.id.et_fuzhugouzaoleixing_qita)
    EditText etFuZhuGouZaoLeiXingQiTa;//辅助用房构造类型其他

    @BindView(R.id.et_fuzhuliefengbianxing)
    EditText etFuZhuLieFeng;//辅助用房裂缝变形倾斜
    @BindView(R.id.et_fuzhuliefengbianxing_qita)
    EditText etFuZhuLieFengQiTa;//辅助用房裂缝变形倾斜其他

    @BindView(R.id.et_fuzhuzhuren)
    EditText etFuZhuZhuRen;//辅助用房是否住人
    @BindView(R.id.et_fuzhuzhuren_qita)
    EditText etFuZhuZhuRenQiTa;//辅助用房是否住人其他

    @BindView(R.id.rg_jielun)
    RadioGroup rgJieLun;//初步结论
    @BindView(R.id.iv_image)
    ImageView ivImage;//图片
    @BindView(R.id.iv_image_delete)
    ImageView ivImageDelete;
    @BindView(R.id.gv_pic)
    GridView gvPic;

    @BindView(R.id.btn_commit)
    Button btnCommit;//提交

    @BindView(R.id.tv_reason)
    TextView tvReason;//审核未通过原因

    @BindView(R.id.ll_item)
    LinearLayout llItem;

    @BindView(R.id.ll_more)
    LinearLayout llMore;
    @BindView(R.id.ll_item_more)
    LinearLayout llItemMore;
    @BindView(R.id.ll_gone)
    LinearLayout llGone;
    @BindView(R.id.ll_item_gone)
    LinearLayout llItemGone;
    @BindView(R.id.tv_gone)
    TextView tvGone;
    @BindView(R.id.iv_gone)
    ImageView ivGone;
    @BindView(R.id.tv_more)
    TextView tvMore;
    @BindView(R.id.iv_more)
    ImageView ivMore;

    @BindView(R.id.rb1)
    CheckBox checkBox1;
    @BindView(R.id.rb2)
    CheckBox checkBox2;
    @BindView(R.id.rb3)
    CheckBox checkBox3;
    private String result1 = "";
    private String result2 = "";
    private String result3 = "";


    private ArrayList<AddressBean> options1Items = new ArrayList<>(); //省
    private ArrayList<ArrayList<String>> options2Items = new ArrayList<>();//市
    private ArrayList<ArrayList<ArrayList<String>>> options3Items = new ArrayList<>();//区

    //点击经纬度
    private String lng;
    private String lat;
    //定位经纬度
//    private String locationLng;
//    private String locationLat;
    private String taskName;
    private String formId;
    private String buildType;

    private String from;
    private boolean isEdit;//标识是否可编辑   true可编辑
    private boolean isFirst;//标识是否是第一次创建表单  true是
    private String userId;
    private String imageUrl;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_village_house_new);
        ButterKnife.bind(this);
        initView();
        initData();
    }

    private void initData(){
        userId = (String) SPUtil.get("userId","");
        lng = getIntent().getStringExtra("lng");
        lat = getIntent().getStringExtra("lat");
//        locationLng = getIntent().getStringExtra("locationLng");
//        locationLat = getIntent().getStringExtra("locationLat");
        taskId = getIntent().getStringExtra("taskId");
        taskName = getIntent().getStringExtra("taskName");
        formId = getIntent().getStringExtra("formId");
        buildType = getIntent().getStringExtra("buildType");

        from = getIntent().getStringExtra("from");

        isEdit = getIntent().getBooleanExtra("isEdit",true);
        isFirst = getIntent().getBooleanExtra("isFirst",false);

        tvSave.setText("保存");
        tvSave.setVisibility(View.VISIBLE);
        tvClear.setText("清空");
        tvClear.setVisibility(View.VISIBLE);
        if(TextUtils.equals(TaskMapActivity.FROM_TASK_MAP,from)){

            if(isFirst){
                initEditTextContent(true);//第一次创建，显示默认值
                getTianDiTuLocation(lng,lat,etBuildingAddress,etProvince,etCity,etArea,etBuildingName,etNumber,null,etXingZhengCun);
            }else {
                getFormData(buildType,formId);
                if(isEdit){
                    //点击地图标识查看表单, 而且可编辑
                }else {
                    //不可编辑
                    setEnabled();
                }
            }
        }else {
            checkBox1.setChecked(false);
            result1 = "";
            getFormData(buildType,formId);
            if(isEdit){
                //点击地图标识查看表单, 而且可编辑
            }else {
                //不可编辑
                setEnabled();
            }

        }

        etLng.setText(lng);
        etLat.setText(lat);

        updateLocation();

    }
    /**
     * 所有控件不可点击编辑
     */
    private void setEnabled() {
        tvSave.setVisibility(View.INVISIBLE);
        tvClear.setVisibility(View.INVISIBLE);
        btnCommit.setVisibility(View.INVISIBLE);

        etBuildingType.setEnabled(false);
        etProvince.setEnabled(false);
        etCity.setEnabled(false);
        etArea.setEnabled(false);

        etBuidingXiangZhen.setEnabled(false);
        etXingZhengCun.setEnabled(false);
        etBuildingName.setEnabled(false);
        etBuildingAddress.setEnabled(false);
        etHuZhuXinXi.setEnabled(false);
        etJieGouLeiXing.setEnabled(false);
        getEtJieGouLeiXingQiTa.setEnabled(false);
        etJianZaoFangShi.setEnabled(false);
        etJianZaoFangShiQiTa.setEnabled(false);
        etZhaiJiDiMianJi.setEnabled(false);
        etJianZhuMianJi.setEnabled(false);
        etJianZhuCengShu.setEnabled(false);
        etJianZhuJianShu.setEnabled(false);
        etJianZhuGaoDu.setEnabled(false);
        etJianZaoShiJian.setEnabled(false);
        etRenKouShu.setEnabled(false);
        etShiYongQingKuang.setEnabled(false);
        etShiYongQingKuangQiTa.setEnabled(false);
        etKangZhenJiaGu.setEnabled(false);
        etKangZhenGouZao.setEnabled(false);
        etGouZaoCuoShiQingKuang.setEnabled(false);
        etGouZaoCuoShiQingKuangQiTa.setEnabled(false);
        etLieFengBianXing.setEnabled(false);
        etLieFengBianXingQiTa.setEnabled(false);
        etShouZaiQingKuang.setEnabled(false);
        etCengShouZaiQingKuang.setEnabled(false);
        etCengShouZaiQingKuangQiTa.setEnabled(false);
        etCengShouZaiQingKuangRemark.setEnabled(false);
        etFuZhuYongFang.setEnabled(false);
        etFuZhuYongTu.setEnabled(false);
        etFuZhuYongTuQiTa.setEnabled(false);
        etFuZhuJianZaoFangShi.setEnabled(false);
        etFuZhuJianZaoFangShiQiTa.setEnabled(false);
        etFuZhuGouZaoLeiXing.setEnabled(false);
        etFuZhuGouZaoLeiXingQiTa.setEnabled(false);
        etFuZhuLieFeng.setEnabled(false);
        etFuZhuLieFengQiTa.setEnabled(false);
        etFuZhuZhuRen.setEnabled(false);
        etFuZhuZhuRenQiTa.setEnabled(false);

        checkBox1.setClickable(false);
        checkBox2.setClickable(false);
        checkBox3.setClickable(false);
        ivImage.setEnabled(false);
        gvPic.setEnabled(false);

    }

    @Override
    public void myLocation(String locationLng, String locationLat) {
        super.myLocation(locationLng, locationLat);
        updateFoot(userId,taskId,locationLng,locationLat);
    }

    private void initView(){
        tvTitle.setText("农村住宅房屋调研表");
        etJieGouLeiXing.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                if(TextUtils.equals("其它",editable.toString())){
                    getEtJieGouLeiXingQiTa.setVisibility(View.VISIBLE);
                }else {
                    getEtJieGouLeiXingQiTa.setVisibility(View.GONE);
                }
            }
        });
        etJianZaoFangShi.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                if(TextUtils.equals("其它",editable.toString())){
                    etJianZaoFangShiQiTa.setVisibility(View.VISIBLE);
                }else {
                    etJianZaoFangShiQiTa.setVisibility(View.GONE);
                }
            }
        });
        etShiYongQingKuang.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                if(TextUtils.equals("其它",editable.toString())){
                    etShiYongQingKuangQiTa.setVisibility(View.VISIBLE);
                }else {
                    etShiYongQingKuangQiTa.setVisibility(View.GONE);
                }
            }
        });
        etGouZaoCuoShiQingKuang.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                if(TextUtils.equals("其他抗震措施",editable.toString())){
                    etGouZaoCuoShiQingKuangQiTa.setVisibility(View.VISIBLE);
                }else {
                    etGouZaoCuoShiQingKuangQiTa.setVisibility(View.GONE);
                }
            }
        });
        etFuZhuYongTu.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                if(TextUtils.equals("其它",editable.toString())){
                    etFuZhuYongTuQiTa.setVisibility(View.VISIBLE);
                }else {
                    etFuZhuYongTuQiTa.setVisibility(View.GONE);
                }
            }
        });
        etFuZhuJianZaoFangShi.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                if(TextUtils.equals("其它",editable.toString())){
                    etFuZhuJianZaoFangShiQiTa.setVisibility(View.VISIBLE);
                }else {
                    etFuZhuJianZaoFangShiQiTa.setVisibility(View.GONE);
                }
            }
        });
        etFuZhuGouZaoLeiXing.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                if(TextUtils.equals("其它",editable.toString())){
                    etFuZhuGouZaoLeiXingQiTa.setVisibility(View.VISIBLE);
                }else {
                    etFuZhuGouZaoLeiXingQiTa.setVisibility(View.GONE);
                }
            }
        });

        etKangZhenGouZao.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                if(TextUtils.equals("基本完备",editable.toString())){
                    llGouZaoCuoShiQingKuang.setVisibility(View.VISIBLE);
                    etGouZaoCuoShiQingKuang.setText(FormDataUtil.getGouZaoCuoShiQingKuang().get(0));
                    etGouZaoCuoShiQingKuangQiTa.setVisibility(View.GONE);
                }else {
                    llGouZaoCuoShiQingKuang.setVisibility(View.GONE);
                    etGouZaoCuoShiQingKuangQiTa.setVisibility(View.GONE);
                    etGouZaoCuoShiQingKuang.setText("");
                    etGouZaoCuoShiQingKuangQiTa.setText("");
                }

            }
        });
        //受灾情况，有受灾显示出来曾受灾情况选择
        etShouZaiQingKuang.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                if(TextUtils.equals("曾受灾",editable.toString())){
                    llCengShouZaiQingKuang.setVisibility(View.VISIBLE);
                    llShouZaiBeiZhu.setVisibility(View.VISIBLE);
                }else {
                    llCengShouZaiQingKuang.setVisibility(View.GONE);
                    llShouZaiBeiZhu.setVisibility(View.GONE);
                }
            }
        });
        etCengShouZaiQingKuang.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if(TextUtils.equals("其它",s.toString())){
                    etCengShouZaiQingKuangQiTa.setVisibility(View.VISIBLE);
                }else {
                    etCengShouZaiQingKuangQiTa.setVisibility(View.GONE);
                }

            }
        });
        etFuZhuYongFang.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                if(TextUtils.equals("有",editable.toString())){
                    llFuZhuYongFang.setVisibility(View.VISIBLE);
                }else {
                    llFuZhuYongFang.setVisibility(View.GONE);
                }
            }
        });
        checkBox1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(isChecked){
                    result1 = Constant.FORM_VILLAGE_RESULT_01;
                }else {
                    result1 = "";
                }
            }
        });
        checkBox2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(isChecked){
                    result2 = Constant.FORM_VILLAGE_RESULT_02;
                }else {
                    result2 = "";
                }
            }
        });
        checkBox3.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(isChecked){
                    result3 = Constant.FORM_VILLAGE_RESULT_03;
                }else {
                    result3 = "";
                }
            }
        });

        //initEditTextContent();

        initLayoutPicture(gvPic);
    }

    private void initEditTextContent(boolean isShowLast){
        //etNumber.setText(new Date().getTime()+"");
        etBuildingType.setText(FormDataUtil.getBuildingType().get(0));
        etJieGouLeiXing.setText(FormDataUtil.getJieGouLeiXing().get(0));
        etJianZaoFangShi.setText(FormDataUtil.getJianZaoFangShi().get(0));
        etJianZaoShiJian.setText(FormDataUtil.getJianZaoShiJian().get(0));
        etShiYongQingKuang.setText(FormDataUtil.getShiYongQingKuang().get(0));

        etKangZhenJiaGu.setText(FormDataUtil.getXianSheFangLieDu().get(0));
        etKangZhenGouZao.setText(FormDataUtil.getKangZhenGouZaoCuoShi().get(0));
        etGouZaoCuoShiQingKuang.setText(FormDataUtil.getGouZaoCuoShiQingKuang().get(0));
        etLieFengBianXing.setText(FormDataUtil.getFuZhuLieFengBianXing().get(0));

        etShouZaiQingKuang.setText(FormDataUtil.getShouZaiQingKuang().get(0));
        etCengShouZaiQingKuang.setText(FormDataUtil.getCengShouZaiQingKuang().get(0));

        etFuZhuYongFang.setText(FormDataUtil.getLieFengBianXing().get(0));
        etFuZhuYongTu.setText(FormDataUtil.getFuZhuYongFangYongTu().get(0));
        etFuZhuJianZaoFangShi.setText(FormDataUtil.getFuZhuJianZaoFangShi().get(0));
        etFuZhuGouZaoLeiXing.setText(FormDataUtil.getFuZhuGouZaoLeiXing().get(0));
        etFuZhuLieFeng.setText(FormDataUtil.getFuZhuLieFengBianXing().get(0));
        etFuZhuZhuRen.setText(FormDataUtil.getFuZhuZhuRen().get(0));


        etBuidingXiangZhen.setText("");
        etXingZhengCun.setText("");
        etBuildingName.setText("");
        etBuildingAddress.setText("");
        etHuZhuXinXi.setText("未知");
        getEtJieGouLeiXingQiTa.setText("");
        etJianZaoFangShiQiTa.setText("");
        etJianZhuMianJi.setText("0");
        etZhaiJiDiMianJi.setText("0");
        etJianZhuCengShu.setText("0");
        etJianZhuJianShu.setText("0");
        etJianZhuGaoDu.setText("0");
        etRenKouShu.setText("0");
        etShiYongQingKuangQiTa.setText("");
        etGouZaoCuoShiQingKuangQiTa.setText("");
        etLieFengBianXingQiTa.setText("");
        etFuZhuYongTuQiTa.setText("");
        etFuZhuJianZaoFangShiQiTa.setText("");
        etFuZhuGouZaoLeiXingQiTa.setText("");
        etFuZhuLieFengQiTa.setText("");
        etFuZhuZhuRenQiTa.setText("");

        checkBox1.setChecked(false);
        checkBox2.setChecked(false);
        checkBox3.setChecked(false);
        result1="";
        result2="";
        result3="";

        imageUrl = "";
        ivImage.setImageResource(R.mipmap.ic_picture);
//        clearImage();

        if(isShowLast){
            String json = (String) SPUtil.get(VILLAGE_HOUSE,"");
            if(!TextUtils.isEmpty(json)) {
                Gson gson = new Gson();
                VillageHouseBean bean = gson.fromJson(json, VillageHouseBean.class);

//            etNumber.setText(bean.getBuildingCode());
//            lng = bean.getBulidingLongitude();
//            lat = bean.getBulidingLatitude();
//            etLng.setText(lng);
//            etLat.setText(lat);
//            etBuildingType.setText(bean.getBulidingType());
//            province = bean.getBelongtoProvince();
//            city = bean.getBelongtoCity();
//            area = bean.getBelongtoArea();
//            if(!TextUtils.isEmpty(province)){
//                etProvince.setText(province);
//            }else {
//                etProvince.setText("");
//            }
//            if(!TextUtils.isEmpty(city)){
//                etCity.setText(city);
//            }else {
//                etCity.setText("");
//            }
//            if(!TextUtils.isEmpty(area)){
//                etArea.setText(area);
//            }else {
//                etArea.setText("");
//            }
                etBuidingXiangZhen.setText(bean.getBelongtoTown());
                etXingZhengCun.setText(bean.getBelongtoVillage());
                //etBuildingName.setText(bean.getBuildingName());
                //etBuildingAddress.setText(bean.getBuildingAddress());
                etHuZhuXinXi.setText(bean.getMasterInfo());

                //结构类型其他
                etJieGouLeiXing.setText(bean.getStructureType());
                getEtJieGouLeiXingQiTa.setText(bean.getStructureTypeOther());

                //建造方式其他
                etJianZaoFangShi.setText(bean.getBuildMethod());
                etJianZaoFangShiQiTa.setText(bean.getBuildMethodOther());

                etZhaiJiDiMianJi.setText(bean.getHomesteadArea()+"");
                etJianZhuMianJi.setText(bean.getBuildingArea()+"");
                etJianZhuCengShu.setText(bean.getBuildingLayerNum()+"");
                etJianZhuJianShu.setText(bean.getBuildingNum()+"");
                etJianZhuGaoDu.setText(bean.getBuildingHight()+"");
                etJianZaoShiJian.setText(bean.getBuildingTime()+"");
                etRenKouShu.setText(bean.getPresonNum()+"");

                //房屋使用情况其他
                etShiYongQingKuang.setText(bean.getHouseUseSituation());
                etShiYongQingKuangQiTa.setText(bean.getHouseUseSituationOther());

                etKangZhenJiaGu.setText(bean.getHouseReinforceSituation());
                etKangZhenGouZao.setText(bean.getHouseMeasures());

                //抗震构造措施情况
                etGouZaoCuoShiQingKuang.setText(bean.getHouseMeasuresSituation());
                etGouZaoCuoShiQingKuangQiTa.setText(bean.getHouseMeasuresSituationOther());

                etLieFengBianXing.setText(bean.getBuildingIsDamage());
                etLieFengBianXingQiTa.setText(bean.getBuildingDamageRemark());

                etShouZaiQingKuang.setText(bean.getHouseDisasterSituation());

                //曾受灾情况其他
                etCengShouZaiQingKuang.setText(bean.getHouseDisasterType());
                etCengShouZaiQingKuangQiTa.setText(bean.getHouseDisasterTypeOther());
                etCengShouZaiQingKuangRemark.setText(bean.getHouseDisasterTypeRemark());

                etFuZhuYongFang.setText(bean.getIsAuxiliaryHouse());

                //辅助房屋用途其他
                etFuZhuYongTu.setText(bean.getAuxiliaryHousePurpose());
                etFuZhuYongTuQiTa.setText(bean.getAuxiliaryHousePurposeOther());

                //辅助用房建造方式其他
                etFuZhuJianZaoFangShi.setText(bean.getAuxiliaryHouseBuildMethod());
                etFuZhuJianZaoFangShiQiTa.setText(bean.getAuxiliaryHouseBuildMethodOther());

                //辅助用房构造类型其他
                etFuZhuGouZaoLeiXing.setText(bean.getAuxiliaryHouseStructure());
                etFuZhuGouZaoLeiXingQiTa.setText(bean.getAuxiliaryHouseStructureOther());

                etFuZhuLieFeng.setText(bean.getAuxiliaryHouseIsDamage());
                etFuZhuLieFengQiTa.setText(bean.getAuxiliaryHouseDamageRemark());
                etFuZhuZhuRen.setText(bean.getIsLivePerpson());
                etFuZhuZhuRenQiTa.setText(bean.getLivePerpsonRemark());

//            imageUrl = bean.getImage();
//            Glide.with(activity).load(imageUrl).asBitmap().error(R.mipmap.ic_picture).into(ivImage);

                String result = bean.getResult();
                if(!TextUtils.isEmpty(result)){
                    String[] array = result.split(",");
                    for(String str : array){
                        switch (str){
                            case Constant.FORM_VILLAGE_RESULT_01:
                                result1 = str;
                                checkBox1.setChecked(true);
                                break;
                            case Constant.FORM_VILLAGE_RESULT_02:
                                result2 = str;
                                checkBox2.setChecked(true);
                                break;
                            case Constant.FORM_VILLAGE_RESULT_03:
                                result3 = str;
                                checkBox3.setChecked(true);
                                break;

                        }
                    }
                }
            }
        }
    }

    @OnClick({R.id.iv_back,R.id.et_building_type,R.id.et_province,R.id.et_city,R.id.et_area,R.id.et_jiegouleixing,R.id.et_jianzaofangshi,R.id.et_jianzaoshijian,R.id.et_shiyongqingkuang,
                R.id.et_kangzhenjiagu,R.id.et_kangzhengouzaocuoshi,R.id.et_gouzaocuoshiqingkuang,R.id.et_liefengbianxing,R.id.et_shouzaiqingkuang,R.id.et_fuzhuyongfang,R.id.et_fuzhuyongtu,
                R.id.et_fuzhujianzaofangshi,R.id.et_fuzhugouzaoleixing,R.id.et_fuzhuliefengbianxing,R.id.et_fuzhuzhuren,R.id.iv_image,R.id.btn_commit,R.id.tv_right_text,R.id.tv_left_text,
                R.id.et_cengshouzaiqingkuang,R.id.ll_item,R.id.iv_image_delete,R.id.ll_gone,R.id.ll_more
    })
    public void onClick(View v) {
        Utils.hintKb(activity);
        switch (v.getId()){
            case R.id.iv_back:
                finish();
                break;
            case R.id.tv_left_text://清空
                initEditTextContent(false);
                break;
            case R.id.ll_gone:
                //隐藏信息
                if(llItemGone.getVisibility() == View.VISIBLE){
                    llItemGone.setVisibility(View.GONE);
                    tvGone.setText("基本信息");
                    ivGone.setImageResource(R.mipmap.ic_bottom_tri);
                }else {
                    llItemGone.setVisibility(View.VISIBLE);
                    tvGone.setText("隐藏信息");
                    ivGone.setImageResource(R.mipmap.ic_top_tri);
                }
                break;
            case R.id.ll_more:
                //更多信息
                if(llItemMore.getVisibility() == View.VISIBLE){
                    llItemMore.setVisibility(View.GONE);
                    ivMore.setImageResource(R.mipmap.ic_bottom_tri);
                }else {
                    llItemMore.setVisibility(View.VISIBLE);
                    ivMore.setImageResource(R.mipmap.ic_top_tri);
                }
                break;
            case R.id.et_building_type://被调查建筑分类
                final List<String> typeList = FormDataUtil.getBuildingType();
                PickViewUtil.showPickerViewOne(activity, "", typeList, new OptionsPickerView.OnOptionsSelectListener() {
                    @Override
                    public void onOptionsSelect(int options1, int options2, int options3, View v) {
                        String type = typeList.get(options1);
                        etBuildingType.setText(type);
                    }
                });
                break;
            case R.id.et_province:
                //省市区
                getDivisions("root",RegisterActivity.PROVINCE,"",null);
                break;
            case R.id.et_city:
                //省市区
                if(provinceBean == null){
                    CustomToast.showToast(activity,"请先选择省");
                    return;
                }
                getDivisions(provinceBean.getDivisionId(),RegisterActivity.CITY,provinceBean.getDivisionType(),provinceBean);
                break;
            case R.id.et_area:
                //省市区
                if(cityBean == null){
                    CustomToast.showToast(activity,"请先选择市");
                    return;
                }
                getDivisions(cityBean.getDivisionId(),RegisterActivity.AREA,"",null);
                break;
//            case R.id.et_building_xiangzhen://乡镇

//                break;
            case R.id.et_jiegouleixing://结构类型
                final List<String> jglxList = FormDataUtil.getVillageJieGouLeiXing();
                PickViewUtil.showPickerViewOne(activity, "", jglxList, new OptionsPickerView.OnOptionsSelectListener() {
                    @Override
                    public void onOptionsSelect(int options1, int options2, int options3, View v) {
                        String type = jglxList.get(options1);
                        etJieGouLeiXing.setText(type);
                    }
                });
                break;
            case R.id.et_jianzaofangshi://建造方式
                final List<String> jzfsList = FormDataUtil.getJianZaoFangShi();
                PickViewUtil.showPickerViewOne(activity, "", jzfsList, new OptionsPickerView.OnOptionsSelectListener() {
                    @Override
                    public void onOptionsSelect(int options1, int options2, int options3, View v) {
                        String type = jzfsList.get(options1);
                        etJianZaoFangShi.setText(type);
                    }
                });
                break;
            case R.id.et_jianzaoshijian://建造时间
                final List<String> jzsjList = FormDataUtil.getJianZaoShiJian();
                PickViewUtil.showPickerViewOne(activity, "", jzsjList, new OptionsPickerView.OnOptionsSelectListener() {
                    @Override
                    public void onOptionsSelect(int options1, int options2, int options3, View v) {
                        String type = jzsjList.get(options1);
                        etJianZaoShiJian.setText(type);
                    }
                });
                break;
            case R.id.et_shiyongqingkuang://房屋使用情况
                final List<String> syqkList = FormDataUtil.getShiYongQingKuang();
                PickViewUtil.showPickerViewOne(activity, "", syqkList, new OptionsPickerView.OnOptionsSelectListener() {
                    @Override
                    public void onOptionsSelect(int options1, int options2, int options3, View v) {
                        String type = syqkList.get(options1);
                        etShiYongQingKuang.setText(type);
                    }
                });
                break;
            case R.id.et_kangzhenjiagu://抗震加固情况
                final List<String> kzjgList = FormDataUtil.getVillageKangZhenJiaGu();
                PickViewUtil.showPickerViewOne(activity, "", kzjgList, new OptionsPickerView.OnOptionsSelectListener() {
                    @Override
                    public void onOptionsSelect(int options1, int options2, int options3, View v) {
                        String type = kzjgList.get(options1);
                        etKangZhenJiaGu.setText(type);
                    }
                });
                break;
            case R.id.et_kangzhengouzaocuoshi://抗震构造措施
                final List<String> kzgzcsList = FormDataUtil.getKangZhenGouZaoCuoShi();
                PickViewUtil.showPickerViewOne(activity, "", kzgzcsList, new OptionsPickerView.OnOptionsSelectListener() {
                    @Override
                    public void onOptionsSelect(int options1, int options2, int options3, View v) {
                        String type = kzgzcsList.get(options1);
                        etKangZhenGouZao.setText(type);
                    }
                });
                break;
            case R.id.et_gouzaocuoshiqingkuang://抗震构造措施情况
                final List<String> gzcsqkList = FormDataUtil.getGouZaoCuoShiQingKuang();
                PickViewUtil.showPickerViewOne(activity, "", gzcsqkList, new OptionsPickerView.OnOptionsSelectListener() {
                    @Override
                    public void onOptionsSelect(int options1, int options2, int options3, View v) {
                        String type = gzcsqkList.get(options1);
                        etGouZaoCuoShiQingKuang.setText(type);
                    }
                });
                break;
            case R.id.et_liefengbianxing://裂缝变形倾斜
                final List<String> lfbx = FormDataUtil.getFuZhuLieFengBianXing();
                PickViewUtil.showPickerViewOne(activity, "", lfbx, new OptionsPickerView.OnOptionsSelectListener() {
                    @Override
                    public void onOptionsSelect(int options1, int options2, int options3, View v) {
                        String type = lfbx.get(options1);
                        etLieFengBianXing.setText(type);
                    }
                });
                break;
            case R.id.et_shouzaiqingkuang://受灾情况
                final List<String> szqkList = FormDataUtil.getShouZaiQingKuang();
                PickViewUtil.showPickerViewOne(activity, "", szqkList, new OptionsPickerView.OnOptionsSelectListener() {
                    @Override
                    public void onOptionsSelect(int options1, int options2, int options3, View v) {
                        String type = szqkList.get(options1);
                        etShouZaiQingKuang.setText(type);
                    }
                });
                break;
            case R.id.et_cengshouzaiqingkuang://房屋曾受灾情况
                final List<String> cszqkList = FormDataUtil.getCengShouZaiQingKuang();
                PickViewUtil.showPickerViewOne(activity, "", cszqkList, new OptionsPickerView.OnOptionsSelectListener() {
                    @Override
                    public void onOptionsSelect(int options1, int options2, int options3, View v) {
                        String type = cszqkList.get(options1);
                        etCengShouZaiQingKuang.setText(type);
                    }
                });
                break;

            case R.id.et_fuzhuyongfang://是否有辅助用房
                final List<String> fzyfList = FormDataUtil.getLieFengBianXing();
                PickViewUtil.showPickerViewOne(activity, "", fzyfList, new OptionsPickerView.OnOptionsSelectListener() {
                    @Override
                    public void onOptionsSelect(int options1, int options2, int options3, View v) {
                        String type = fzyfList.get(options1);
                        etFuZhuYongFang.setText(type);
                    }
                });
                break;
            case R.id.et_fuzhuyongtu://辅助用房用途
                final List<String> fzyfytList = FormDataUtil.getFuZhuYongFangYongTu();
                PickViewUtil.showPickerViewOne(activity, "", fzyfytList, new OptionsPickerView.OnOptionsSelectListener() {
                    @Override
                    public void onOptionsSelect(int options1, int options2, int options3, View v) {
                        String type = fzyfytList.get(options1);
                        etFuZhuYongTu.setText(type);
                    }
                });
                break;
            case R.id.et_fuzhujianzaofangshi://辅助用房建造方式
                final List<String> fzjzfsList = FormDataUtil.getFuZhuJianZaoFangShi();
                PickViewUtil.showPickerViewOne(activity, "", fzjzfsList, new OptionsPickerView.OnOptionsSelectListener() {
                    @Override
                    public void onOptionsSelect(int options1, int options2, int options3, View v) {
                        String type = fzjzfsList.get(options1);
                        etFuZhuJianZaoFangShi.setText(type);
                    }
                });
                break;
            case R.id.et_fuzhugouzaoleixing://辅助用房构造类型
                final List<String> fzgzlxList = FormDataUtil.getFuZhuGouZaoLeiXing();
                PickViewUtil.showPickerViewOne(activity, "", fzgzlxList, new OptionsPickerView.OnOptionsSelectListener() {
                    @Override
                    public void onOptionsSelect(int options1, int options2, int options3, View v) {
                        String type = fzgzlxList.get(options1);
                        etFuZhuGouZaoLeiXing.setText(type);
                    }
                });
                break;
            case R.id.et_fuzhuliefengbianxing://辅助用房裂缝变形倾斜
                final List<String> fzlfbxList = FormDataUtil.getFuZhuLieFengBianXing();
                PickViewUtil.showPickerViewOne(activity, "", fzlfbxList, new OptionsPickerView.OnOptionsSelectListener() {
                    @Override
                    public void onOptionsSelect(int options1, int options2, int options3, View v) {
                        String type = fzlfbxList.get(options1);
                        etFuZhuLieFeng.setText(type);
                    }
                });
                break;
            case R.id.et_fuzhuzhuren://辅助用房是否住人
                final List<String> fzzrList = FormDataUtil.getFuZhuZhuRen();
                PickViewUtil.showPickerViewOne(activity, "", fzzrList, new OptionsPickerView.OnOptionsSelectListener() {
                    @Override
                    public void onOptionsSelect(int options1, int options2, int options3, View v) {
                        String type = fzzrList.get(options1);
                        etFuZhuZhuRen.setText(type);
                    }
                });
                break;
            case R.id.iv_image:
//                if(myDialog == null){
//                    myDialog = DialogUtils.showRefreshDialog(activity);
//                }else {
//                    myDialog.show();
//                }
                selectImage();
                break;
            case R.id.iv_image_delete:
                //删除图片
                imageUrl = "";
                Glide.with(activity).load(imageUrl).asBitmap().error(R.mipmap.ic_picture).into(ivImage);
                break;
            case R.id.btn_commit://提交
                String content = isComplete();
                if(!TextUtils.isEmpty(content)){
                    CustomToast.showToast(activity,content);
                    return;
                }
                DialogUtils.showIOSDialog(activity, "提示", "", "", "", "", null,new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        updateLocation();//上传活动轨迹

                        if(myDialog == null){
                            myDialog = DialogUtils.showRefreshDialog(activity);
                        }else {
                            myDialog.show();
                        }
                        myDialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
                            @Override public void onDismiss(DialogInterface dialog) {
                                btnCommit.setEnabled(true);
                            }
                        });

                        btnCommit.setEnabled(false);
                        //0、暂存1、提交
                        //是否填完0、否1、是
                        commit("1","1");

                    }
                },"确定要提交当前表单？");


                break;
            case R.id.tv_right_text:
                if(myDialog == null){
                    myDialog = DialogUtils.showRefreshDialog(activity);
                }else {
                    myDialog.show();
                }
                if(TextUtils.isEmpty(etNumber.getText().toString())){
                    //建筑物编号为空，不能保存操作
                    CustomToast.showToast(activity,"没有建筑物编号，无法保存，请重新进入该界面");
                    return;
                }

                tvSave.setEnabled(false);

                //0、暂存1、提交
                //是否填完0、否  1、是
                String str = isComplete();
                updateLocation();//上传活动轨迹
                String isComplete = "0";
                if(TextUtils.isEmpty(str)){
                    //填完整的保存
                    isComplete = "1" ;
                }else {
                    isComplete = "0";
                }
                commit("0",isComplete);
                break;
        }
    }

    private String isComplete(){
        String content = "";
        if(TextUtils.equals("已拆除",etBuildingType.getText().toString())){
            if(TextUtils.isEmpty(etNumber.getText().toString())){

                content = "请输入建筑物编号";
                return content;
            }
            if(TextUtils.isEmpty(etLng.getText().toString())){
                content = "请输入经度";
                return content;
            }
            if(TextUtils.isEmpty(etLat.getText().toString())){
                content = "请输入纬度";
                return content;
            }
        }else {
            if(TextUtils.isEmpty(etNumber.getText().toString())){

                content = "请输入建筑物编号";
                return content;
            }
            if(TextUtils.isEmpty(etLng.getText().toString())){
                content = "请输入经度";
                return content;
            }
            if(TextUtils.isEmpty(etLat.getText().toString())){
                content = "请输入纬度";
                return content;
            }
            if(TextUtils.isEmpty(etProvince.getText().toString())){
                content = "请选择省";
                return content;
            }
            if(TextUtils.isEmpty(etCity.getText().toString())){
                content = "请选择市";
                return content;
            }
            if(TextUtils.isEmpty(etArea.getText().toString())){
                content = "请选择区";
                return content;
            }
            if(TextUtils.isEmpty(etBuidingXiangZhen.getText().toString())){
                content = "请输入乡镇";
                return content;
            }if(TextUtils.isEmpty(etXingZhengCun.getText().toString())){
                content = "请输入行政村";
                return content;
            }

            if(TextUtils.isEmpty(etBuildingName.getText().toString())){
                content = "请输入建筑名称";
                return content;
            }
            if(TextUtils.isEmpty(etBuildingAddress.getText().toString())){
                content = "请输入建筑地址";
                return content;
            }
            if(TextUtils.isEmpty(etHuZhuXinXi.getText().toString())){
                content = "请输入户主信息";
                return content;
            }

            if(TextUtils.isEmpty(etJieGouLeiXing.getText().toString())){
                content = "请选择结构类型";
                return content;
            }
            if(TextUtils.equals("其它",etJieGouLeiXing.getText().toString())){
                if(TextUtils.isEmpty(getEtJieGouLeiXingQiTa.getText().toString())){
                    content = "请输入其它结构类型的补充内容";
                    return content;
                }
            }
            if(TextUtils.isEmpty(etJianZaoFangShi.getText().toString())){
                content = "请选择建造方式";
                return content;
            }
            if(TextUtils.equals("其它",etJianZaoFangShi.getText().toString())){
                if(TextUtils.isEmpty(etJianZaoFangShiQiTa.getText().toString())){
                    content = "请输入其它建造方式的补充内容";
                    return content;
                }
            }

            if(TextUtils.isEmpty(etZhaiJiDiMianJi.getText().toString())){
                content = "请输入宅面积";
                return content;
            }
            if(TextUtils.isEmpty(etJianZhuMianJi.getText().toString())){
                content = "请输入房屋建筑面积";
                return content;
            }
            if(TextUtils.isEmpty(etJianZhuCengShu.getText().toString())){
                content = "请输入房屋建筑层数";
                return content;
            }
            if(TextUtils.isEmpty(etJianZhuJianShu.getText().toString())){
                content = "请输入房屋建筑间数";
                return content;
            }
            if(TextUtils.isEmpty(etJianZhuGaoDu.getText().toString())){
                content = "请输入建筑高度";
                return content;
            }
            if(TextUtils.isEmpty(etJianZaoShiJian.getText().toString())){
                content = "请选择建造时间";
                return content;
            }
            if(TextUtils.isEmpty(etRenKouShu.getText().toString())){
                content = "请输入常住人口数";
                return content;
            }
            if(TextUtils.isEmpty(etShiYongQingKuang.getText().toString())){
                content = "请选择房屋使用情况";
                return content;
            }
            if(TextUtils.equals("其它",etShiYongQingKuang.getText().toString())){
                if(TextUtils.isEmpty(etShiYongQingKuangQiTa.getText().toString())){
                    content = "请输入其它使用情况的补充内容";
                    return content;
                }
            }

            if(TextUtils.equals("基本完备",etKangZhenGouZao.getText().toString())){
                //基本完备需要 传递 措施情况
                if(TextUtils.equals("其他抗震措施",etGouZaoCuoShiQingKuang.getText().toString())){
                    if(TextUtils.isEmpty(etGouZaoCuoShiQingKuangQiTa.getText().toString())){
                        content = "请输入其它抗震构造措施情况的补充";
                        return content;
                    }
                }
            }

            if(TextUtils.equals("曾受灾",etShouZaiQingKuang.getText().toString())){
                //基本完备需要 传递 措施情况
                if(TextUtils.equals("其它",etCengShouZaiQingKuang.getText().toString())){
                    if(TextUtils.isEmpty(etCengShouZaiQingKuangQiTa.getText().toString())){
                        content = "请输入其它曾受灾情况的补充";
                        return content;
                    }
                }
            }
            if(TextUtils.equals("有",etFuZhuYongFang.getText().toString())){
                //有辅助用房
                if(TextUtils.equals("其它",etFuZhuYongTu.getText().toString())){
                    if(TextUtils.isEmpty(etFuZhuYongTuQiTa.getText().toString())){
                        content = "请输入辅助用房用途的其它补充";
                        return content;
                    }
                }

                if(TextUtils.equals("其它",etFuZhuJianZaoFangShi.getText().toString())){
                    if(TextUtils.isEmpty(etFuZhuJianZaoFangShiQiTa.getText().toString())){
                        content = "请输入辅助用房建造方式的其它补充";
                        return content;
                    }
                }
                if(TextUtils.equals("其它",etFuZhuGouZaoLeiXing.getText().toString())){
                    if(TextUtils.isEmpty(etFuZhuGouZaoLeiXingQiTa.getText().toString())){
                        content = "请输入辅助用房构造类型的其它补充";
                        return content;
                    }
                }
            }
            if(TextUtils.isEmpty(getResult())){
                content = "请选择初步结论";
                return content;
            }
//            if(TextUtils.isEmpty(imageUrl)){
//                content = "请选择图片";
//                return content;
//            }
        }

        return content;

    }

    private void getFormData(final String buildType, String formId) {
        String url = Api.FORM+"?type="+buildType+"&formId="+formId;
        MyHttpClient
                .newBuilder()
                .get()
                .url(url)
//                .addParam(json)
                .build()
                .enqueue(new BaseCallBack<VillageHouseResultBean>() {
                    @Override
                    public void onSuccess(VillageHouseResultBean resultBean) {
                        super.onSuccess(resultBean);
                        if(resultBean.getCode() == 200 && resultBean.getData() != null){
                            VillageHouseBean bean = resultBean.getData();
                            etNumber.setText(bean.getBuildingCode());
                            buildingCode = bean.getBuildingCode();
                            lng = bean.getBulidingLongitude();
                            lat = bean.getBulidingLatitude();
                            etLng.setText(lng);
                            etLat.setText(lat);
                            etBuildingType.setText(bean.getBulidingType());
                            province = bean.getBelongtoProvince();
                            city = bean.getBelongtoCity();
                            area = bean.getBelongtoArea();
                            if(!TextUtils.isEmpty(province)){
                                etProvince.setText(province);
                            }else {
                                etProvince.setText("");
                            }
                            if(!TextUtils.isEmpty(city)){
                                etCity.setText(city);
                            }else {
                                etCity.setText("");
                            }
                            if(!TextUtils.isEmpty(area)){
                                etArea.setText(area);
                            }else {
                                etArea.setText("");
                            }
                            etBuidingXiangZhen.setText(bean.getBelongtoTown());
                            etXingZhengCun.setText(bean.getBelongtoVillage());
                            etBuildingName.setText(bean.getBuildingName());
                            etBuildingAddress.setText(bean.getBuildingAddress());
                            etHuZhuXinXi.setText(bean.getMasterInfo());

                            //结构类型其他
                            etJieGouLeiXing.setText(bean.getStructureType());
                            getEtJieGouLeiXingQiTa.setText(bean.getStructureTypeOther());

                            //建造方式其他
                            etJianZaoFangShi.setText(bean.getBuildMethod());
                            etJianZaoFangShiQiTa.setText(bean.getBuildMethodOther());

                            etZhaiJiDiMianJi.setText(bean.getHomesteadArea()+"");
                            etJianZhuMianJi.setText(bean.getBuildingArea()+"");
                            etJianZhuCengShu.setText(bean.getBuildingLayerNum()+"");
                            etJianZhuJianShu.setText(bean.getBuildingNum()+"");
                            etJianZhuGaoDu.setText(bean.getBuildingHight()+"");
                            etJianZaoShiJian.setText(bean.getBuildingTime()+"");
                            etRenKouShu.setText(bean.getPresonNum()+"");

                            //房屋使用情况其他
                            etShiYongQingKuang.setText(bean.getHouseUseSituation());
                            etShiYongQingKuangQiTa.setText(bean.getHouseUseSituationOther());

                            etKangZhenJiaGu.setText(bean.getHouseReinforceSituation());
                            etKangZhenGouZao.setText(bean.getHouseMeasures());

                            //抗震构造措施情况
                            etGouZaoCuoShiQingKuang.setText(bean.getHouseMeasuresSituation());
                            etGouZaoCuoShiQingKuangQiTa.setText(bean.getHouseMeasuresSituationOther());

                            etLieFengBianXing.setText(bean.getBuildingIsDamage());
                            etLieFengBianXingQiTa.setText(bean.getBuildingDamageRemark());

                            etShouZaiQingKuang.setText(bean.getHouseDisasterSituation());

                            //曾受灾情况其他
                            etCengShouZaiQingKuang.setText(bean.getHouseDisasterType());
                            etCengShouZaiQingKuangQiTa.setText(bean.getHouseDisasterTypeOther());
                            etCengShouZaiQingKuangRemark.setText(bean.getHouseDisasterTypeRemark());

                            etFuZhuYongFang.setText(bean.getIsAuxiliaryHouse());

                            //辅助房屋用途其他
                            etFuZhuYongTu.setText(bean.getAuxiliaryHousePurpose());
                            etFuZhuYongTuQiTa.setText(bean.getAuxiliaryHousePurposeOther());

                            //辅助用房建造方式其他
                            etFuZhuJianZaoFangShi.setText(bean.getAuxiliaryHouseBuildMethod());
                            etFuZhuJianZaoFangShiQiTa.setText(bean.getAuxiliaryHouseBuildMethodOther());

                            //辅助用房构造类型其他
                            etFuZhuGouZaoLeiXing.setText(bean.getAuxiliaryHouseStructure());
                            etFuZhuGouZaoLeiXingQiTa.setText(bean.getAuxiliaryHouseStructureOther());

                            etFuZhuLieFeng.setText(bean.getAuxiliaryHouseIsDamage());
                            etFuZhuLieFengQiTa.setText(bean.getAuxiliaryHouseDamageRemark());
                            etFuZhuZhuRen.setText(bean.getIsLivePerpson());
                            etFuZhuZhuRenQiTa.setText(bean.getLivePerpsonRemark());

                            imageUrl = bean.getImage();
//                            Glide.with(activity).load(imageUrl).asBitmap().error(R.mipmap.ic_picture).into(ivImage);
//                            updateImage(new ArrayList<ImageURL>());
                            if(!TextUtils.isEmpty(imageUrl)){
                                String[] arrImage = imageUrl.split(",");
                                if(listUrl == null){
                                    listUrl = new ArrayList<ImageURL>();
                                }
                                for(String url : arrImage){
                                    ImageURL imageURL = new ImageURL();
                                    imageURL.setLocalPath(url);
                                    imageURL.setThumbnail(url);
                                    listUrl.add(imageURL);
                                }
                                updateImage(listUrl);
                            }


                            //是否显示审核未通过提示
                            if(!TextUtils.isEmpty(bean.getReason())){
                                tvReason.setVisibility(View.VISIBLE);
                                tvReason.setText(bean.getReason());
                            }else {
                                tvReason.setVisibility(View.GONE);
                            }

                            String result = bean.getResult();
                            if(!TextUtils.isEmpty(result)){
                                String[] array = result.split(",");
                                for(String str : array){
                                    switch (str){
                                        case Constant.FORM_VILLAGE_RESULT_01:
                                            result1 = str;
                                            checkBox1.setChecked(true);
                                            break;
                                        case Constant.FORM_VILLAGE_RESULT_02:
                                            result2 = str;
                                            checkBox2.setChecked(true);
                                            break;
                                        case Constant.FORM_VILLAGE_RESULT_03:
                                            result3 = str;
                                            checkBox3.setChecked(true);
                                            break;

                                    }
                                }
                            }
                        }else {
                            CustomToast.showToast(activity, "获取表单内容失败");
                        }
                        if (myDialog != null)
                            myDialog.dismiss();
                    }

                    @Override
                    public void onFailure(Call call, IOException e) {
                        super.onFailure(call, e);
                        if (myDialog != null)
                            myDialog.dismiss();
                        CustomToast.showToast(activity, "网络异常");
                    }

                    @Override
                    public void onError(int code) {
                        super.onError(code);
                        if (myDialog != null)
                            myDialog.dismiss();
                        CustomToast.showToast(activity, "网络异常");
                    }
                });

    }


    private void commit(String type,String isComplete) {
        String url = Api.FORM_TOWN_HOUSE+type+"/updVillage";
        if(isFirst){
            //从地图第一次过来提交的接口
            url = Api.FORM_TOWN_HOUSE+type+"/villageHouseFrom";
        }
        VillageHouseBean bean = new VillageHouseBean();
        if(!TextUtils.isEmpty(formId)){
            bean.setFwId(formId);
        }
        bean.setIsSaveAll(isComplete);//保存时是否是全部填写完
        bean.setTaskId(taskId);
        bean.setTaskName(taskName);
        bean.setBuildingCode(etNumber.getText().toString());
        bean.setBulidingLongitude(lng);
        bean.setBulidingLatitude(lat);
        bean.setBulidingType(etBuildingType.getText().toString());
        bean.setBelongtoProvince(province);
        bean.setBelongtoCity(city);
        bean.setBelongtoArea(area);
        bean.setBelongtoTown(etBuidingXiangZhen.getText().toString());
        bean.setBelongtoVillage(etXingZhengCun.getText().toString());
        bean.setBuildingName(etBuildingName.getText().toString());
        bean.setBuildingAddress(etBuildingAddress.getText().toString());
        //户主信息
        bean.setMasterInfo(etHuZhuXinXi.getText().toString());
        //结构类型
        if(TextUtils.equals("其它",etJieGouLeiXing.getText().toString())){
            bean.setStructureType(etJieGouLeiXing.getText().toString());
            bean.setStructureTypeOther(getEtJieGouLeiXingQiTa.getText().toString());
        }else {
            bean.setStructureType(etJieGouLeiXing.getText().toString());
        }


        if(TextUtils.equals("其它",etJianZaoFangShi.getText().toString())){
            bean.setBuildMethodOther(etJianZaoFangShiQiTa.getText().toString());
            bean.setBuildMethod(etJianZaoFangShi.getText().toString());

        }else {
            bean.setBuildMethod(etJianZaoFangShi.getText().toString());
        }


        if(TextUtils.isEmpty(etZhaiJiDiMianJi.getText().toString())){
            etZhaiJiDiMianJi.setText("0");
        }

        if(TextUtils.isEmpty(etJianZhuMianJi.getText().toString())){
            etJianZhuMianJi.setText("0");
        }
        if(TextUtils.isEmpty(etJianZhuCengShu.getText().toString())){
            etJianZhuCengShu.setText("0");
        }
        if(TextUtils.isEmpty(etJianZhuJianShu.getText().toString())){
            etJianZhuJianShu.setText("0");
        }
        if(TextUtils.isEmpty(etJianZhuGaoDu.getText().toString())){
            etJianZhuGaoDu.setText("0");
        }
        bean.setHomesteadArea(etZhaiJiDiMianJi.getText().toString());
        bean.setBuildingArea(Integer.parseInt(etJianZhuMianJi.getText().toString()));
        bean.setBuildingLayerNum(Integer.parseInt(etJianZhuCengShu.getText().toString()));
        bean.setBuildingNum(Integer.parseInt(etJianZhuJianShu.getText().toString()));
        bean.setBuildingHight(Integer.parseInt(etJianZhuGaoDu.getText().toString()));

        bean.setBuildingTime(etJianZaoShiJian.getText().toString());
        if(TextUtils.isEmpty(etRenKouShu.getText().toString())){
            etRenKouShu.setText("0");
        }
        bean.setPresonNum(Integer.parseInt(etRenKouShu.getText().toString()));

        if(TextUtils.equals("其它",etShiYongQingKuang.getText().toString())){
            bean.setHouseUseSituation(etShiYongQingKuang.getText().toString());
            bean.setHouseUseSituationOther(etShiYongQingKuangQiTa.getText().toString());
        }else {
            bean.setHouseUseSituation(etShiYongQingKuang.getText().toString());
        }



        bean.setHouseReinforceSituation(etKangZhenJiaGu.getText().toString());
        bean.setHouseMeasures(etKangZhenGouZao.getText().toString());
        if(TextUtils.equals("基本完备",etKangZhenGouZao.getText().toString())){
            //完备的话，需要填写措施情况
            if(TextUtils.equals("其他抗震措施",etGouZaoCuoShiQingKuang.getText().toString())){
                bean.setHouseMeasuresSituation(etGouZaoCuoShiQingKuang.getText().toString());
                bean.setHouseMeasuresSituationOther(etGouZaoCuoShiQingKuangQiTa.getText().toString());
            }else {
                bean.setHouseMeasuresSituation(etGouZaoCuoShiQingKuang.getText().toString());
            }
        }else {
            bean.setHouseMeasuresSituation("");
            bean.setHouseMeasuresSituationOther("");
        }
        bean.setBuildingIsDamage(etLieFengBianXing.getText().toString());
        bean.setBuildingDamageRemark(etLieFengBianXingQiTa.getText().toString());

        bean.setHouseDisasterSituation(etShouZaiQingKuang.getText().toString());
        if(TextUtils.equals("曾受灾",etShouZaiQingKuang.getText().toString())){
            //曾受灾，需要填写曾受灾情况
            if(TextUtils.equals("其它",etCengShouZaiQingKuang.getText().toString())){
                bean.setHouseDisasterTypeOther(etCengShouZaiQingKuangQiTa.getText().toString());
                bean.setHouseDisasterType(etCengShouZaiQingKuang.getText().toString());
            }else {
                bean.setHouseDisasterType(etCengShouZaiQingKuang.getText().toString());
            }
        }
        bean.setHouseDisasterTypeRemark(etCengShouZaiQingKuangRemark.getText().toString());

        bean.setIsAuxiliaryHouse(etFuZhuYongFang.getText().toString());
        if(TextUtils.equals("有",etFuZhuYongFang.getText().toString())){
            //有辅助用房
            if(TextUtils.equals("其它",etFuZhuYongTu.getText().toString())){
                bean.setAuxiliaryHousePurposeOther(etFuZhuYongTuQiTa.getText().toString());
                bean.setAuxiliaryHousePurpose(etFuZhuYongTu.getText().toString());
            }else {
                bean.setAuxiliaryHousePurpose(etFuZhuYongTu.getText().toString());
            }



            if(TextUtils.equals("其它",etFuZhuJianZaoFangShi.getText().toString())){
                bean.setAuxiliaryHouseBuildMethodOther(etFuZhuJianZaoFangShiQiTa.getText().toString());
                bean.setAuxiliaryHouseBuildMethod(etFuZhuJianZaoFangShi.getText().toString());
            }else {
                bean.setAuxiliaryHouseBuildMethod(etFuZhuJianZaoFangShi.getText().toString());
            }



            if(TextUtils.equals("其它",etFuZhuGouZaoLeiXing.getText().toString())){
                bean.setAuxiliaryHouseStructure(etFuZhuGouZaoLeiXing.getText().toString());
                bean.setAuxiliaryHouseStructureOther(etFuZhuGouZaoLeiXingQiTa.getText().toString());
            }else {
                bean.setAuxiliaryHouseStructure(etFuZhuGouZaoLeiXing.getText().toString());
            }



            bean.setAuxiliaryHouseIsDamage(etFuZhuLieFeng.getText().toString());
            bean.setAuxiliaryHouseDamageRemark(etFuZhuLieFengQiTa.getText().toString());
            bean.setIsLivePerpson(etFuZhuZhuRen.getText().toString());
            bean.setLivePerpsonRemark(etFuZhuZhuRenQiTa.getText().toString());
        }

        imageUrl = "";
        if(listUrl != null){
            for(int i=0; i<listUrl.size(); i++){
                if(i == listUrl.size() -1){
                    imageUrl = imageUrl + listUrl.get(i).getThumbnail();
                }else {
                    imageUrl =  imageUrl + listUrl.get(i).getThumbnail() + ",";
                }
            }
        }
        bean.setImage(imageUrl);

        String result = getResult();
        bean.setResult(result);

        Gson gson = new Gson();
        String json = gson.toJson(bean);

        SPUtil.save(VILLAGE_HOUSE,json);

        MyHttpClient
                .newBuilder()
                .post()
                .url(url)
                .addParam(json)
                .build()
                .enqueue(new BaseCallBack<LoginBean>() {
                    @Override
                    public void onSuccess(LoginBean bean) {
                        super.onSuccess(bean);
//{"code":200,"data":{"success":true,"errorCode":null,"errorMessage":null,"retryTimes":0,"remainLockedTime":null,"userId":"ca3ca0d0d557424d94ec8009b6ca6d08","token":"eyJ0eXBlIjoiSldUIiwiYWxnIjoiSFMyNTYifQ.eyJqdGkiOiJhNGQ1MTkxZC01ZjY0LTQ5YjEtYTk4NS01NjM2YTA5ZDc1M2EiLCJpYXQiOjE1ODk1MzMxNDksInN1YiI6IjkzNWQ4MzNkZGU0MjRlMWJhYzdmZWVjM2M0MDZhMDI5IiwiZXhwIjoxNTg5NTMzNTQ5fQ.KGhav1E8SLuHXkb60RsbWj3xLMscIPrGmvDSmPjSLr4"},"message":null}
                        if(bean.getCode() == 200){
                            if(TextUtils.equals(TaskMapActivity.FROM_TASK_MAP,from)){
                                //地图过来的，回到任务列表
                                //startActivity(new Intent(activity,TaskListActivity.class));
                                setResult(RESULT_OK);
                                finish();
                            }else {
                                //表单列表过来的回到 表单列表  刷新列表
                                setResult(RESULT_OK);
                                finish();
                            }
                        }else {
                            tvSave.setEnabled(true);
                        }
                        if (myDialog != null)
                            myDialog.dismiss();

                    }

                    @Override
                    public void onFailure(Call call, IOException e) {
                        super.onFailure(call, e);
                        if (myDialog != null)
                            myDialog.dismiss();
                        CustomToast.showToast(activity, "网络异常");
                        tvSave.setEnabled(true);

                    }

                    @Override
                    public void onError(int code) {
                        super.onError(code);
                        if (myDialog != null)
                            myDialog.dismiss();
                        CustomToast.showToast(activity, "网络异常");
                        tvSave.setEnabled(true);

                    }
                });

    }

    /**
     * 最终的初步结论
     * @return
     */
    private String getResult(){
        String str = "";
        if(!TextUtils.isEmpty(result1)){
            str = str+result1+",";
        }
        if(!TextUtils.isEmpty(result2)){
            str = str+result2+",";
        }
        if(!TextUtils.isEmpty(result3)){
            str = str+result3+",";
        }

        if(!TextUtils.isEmpty(str)){
            str = str.substring(0,str.length()-1);
        }
        return str;
    }



    /**
     * 调用天地图获取地理信息
     */
    private void getTianDiTuLocation(String lng,String lat) {

        String url = "http://api.tianditu.gov.cn/geocoder?postStr={'lon':"+lng+",'lat':"+lat+",'ver':1}&type=geocode&tk="+Constant.TIAN_DI_TU_ANDROID_KEY;
//        String url = "http://api.tianditu.gov.cn/geocoder?postStr={%27lon%27:116.28,%27lat%27:40.1419,%27ver%27:1}&type=geocode&tk=3bd24441a20ee975f046a8d7417b936a";
        final JsonBuilder jsonBuilder = new JsonBuilder(this);
//        jsonBuilder.put("doctorMobile", etPhone.getText().toString());

        MyHttpClient
                .newBuilder()
                .get()
                .url(url)
//                .addParam(jsonBuilder.toJsonString())
                .build()
                .enqueue(new BaseCallBack<String>() {
                    @Override
                    public void onSuccess(String response) {
                        super.onSuccess(response);
                        try {
                            JSONObject jsonObject = new JSONObject(response);
                            if(TextUtils.equals(jsonObject.getString("msg"),"ok")){
                                JSONObject object = jsonObject.getJSONObject("result").getJSONObject("addressComponent");
                                String formatted_address = jsonObject.getJSONObject("result").getString("formatted_address");//建筑地址
                                String province = object.getString("province");//省
                                String city = object.getString("city");//市
                                String county = object.getString("county");//区
//                                String road = object.getString("road");//街道
                                String poi = object.getString("poi");//建筑名称

                                if(!TextUtils.isEmpty(formatted_address)){
                                    etBuildingAddress.setText(formatted_address);
                                }
                                if(!TextUtils.isEmpty(province)){
                                    VillageHouseFormActivity.this.province = province;
                                    VillageHouseFormActivity.this.city = city;
                                    VillageHouseFormActivity.this.area = county;
                                    etProvince.setText(province);
                                    etCity.setText(city);
                                    etArea.setText(area);                                }
//                                if(!TextUtils.isEmpty(road)){
////                                    etBuidingStreet.setText(road);
//                                }
                                if(!TextUtils.isEmpty(poi)){
                                    etBuildingName.setText(poi);
                                }
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }

                    @Override
                    public void onFailure(Call call, IOException e) {
                        super.onFailure(call, e);
//                        if (myDialog != null)
//                            myDialog.dismiss();
                    }

                    @Override
                    public void onError(int code) {
                        super.onError(code);
//                        if (myDialog != null)
//                            myDialog.dismiss();
                    }
                });

    }

    @Override
    public void setProvinceText(String province) {
        super.setProvinceText(province);
        etProvince.setText(province);
    }

    @Override
    public void setCityText(String city) {
        super.setCityText(city);
        etCity.setText(city);
    }

    @Override
    public void setAreaText(String area) {
        super.setAreaText(area);
        etArea.setText(area);
    }

    @Override
    public void setImage(String url) {
        super.setImage(url);
//        imageUrl = url;
//        Glide.with(activity).load(imageUrl).asBitmap().error(R.mipmap.ic_picture).into(ivImage);
    }
}
